import { SocketServer } from '../util/SocketServer';
import { GlobalData } from '../global/GlobalData';

export class SocketManager
{
    private static _instance:SocketManager;

    private _wss:SocketServer;

    constructor()
    {

    }

    public static getInstance():SocketManager
    {
        if(this._instance == null)
        {
            this._instance = new SocketManager();
        }

        return this._instance;
    }

    /**初始化 */
    public init():void
    {
        this._wss = new SocketServer();
        this._wss.init(GlobalData.selfInfo.port);
    }

    /**解析socket请求 */
    public messageParse(ws:WebSocket,msg:any):void
    {
        console.log('DB receive:',msg);
        let obj:com.BaseProto = JSON.parse(msg);
        console.log('head_name:',obj.head_name);
        console.log('body_name:',obj.body_name);
        // Operater.query();
        switch(obj.head_name)
        {
            case com.HeadName.login:
                LoginManager.getInstance().messageParse(ws,obj);
                break;
        }
    }


    /**主动停止服务 */
    public dispose():void
    {

    }
}